using System;
using UnityEngine;
using UnityEngine.UI;
using Utils;
using Utils.JsonModule;

public class LoginPanel : MonoBehaviour
{
    private Button startBtn, loadBtn, storyBtn, characterBtn, settingBtn, exitBtn;

    private void Awake()
    {
        startBtn = UITool.GetComponent<Button>("Btns/StartBtn", gameObject);
        loadBtn = UITool.GetComponent<Button>("Btns/LoadBtn", gameObject);
        storyBtn = UITool.GetComponent<Button>("Btns/StoryBtn", gameObject);
        characterBtn = UITool.GetComponent<Button>("Btns/CharacterBtn", gameObject);
        settingBtn = UITool.GetComponent<Button>("Btns/SettingBtn", gameObject);
        exitBtn = UITool.GetComponent<Button>("Btns/ExitBtn", gameObject);
        
        AddButtonClickEvent();

    }

    void OnEnable()
    {
        try
        {
            LevelData levelData2 = JsonHandler.GetObjectFromJson<LevelData>(FilePathConfigue.LevelData);
            if (levelData2.state)
            {
                UITool.GetGameObjectByPath("Btns/LoadBtn", gameObject).SetActive(false);
            }
        }
        catch (Exception e)
        {
            UITool.GetGameObjectByPath("Btns/LoadBtn", gameObject).SetActive(false);
            // UITool.GetComponent<GridLayoutGroup>("Btns", gameObject);
        }
    }

    private void OnDisable()
    {
        startBtn.onClick.RemoveListener(NewGameLevel);
        loadBtn.onClick.RemoveListener(LoadGameLevel);
        storyBtn.onClick.RemoveListener(StoryMode);
        characterBtn.onClick.RemoveListener(OpenCharacter);
        settingBtn.onClick.RemoveListener(Setting);
        exitBtn.onClick.RemoveListener(ExitGame);
    }

    private void AddButtonClickEvent()
    {
        startBtn.onClick.AddListener(NewGameLevel);
        loadBtn.onClick.AddListener(LoadGameLevel);
        storyBtn.onClick.AddListener(StoryMode);
        characterBtn.onClick.AddListener(OpenCharacter);
        settingBtn.onClick.AddListener(Setting);
        exitBtn.onClick.AddListener(ExitGame);
    }

    private void NewGameLevel()
    {
        GameController.Instance().archiveCtr.levelData.gameType = GameType.Dungeon;
        GameController.Instance().mapCtr.CreateChildMaps();
        GameController.Instance().playerCtr.CreatePlayer();
        GameController.Instance().weaponCtr.CreateWeapon();

        GameController.Instance().archiveCtr.CreateNewArchive();
        Destroy(gameObject);
    }

    private void LoadGameLevel()
    {
        GameController.Instance().archiveCtr.LoadArchive();
        Destroy(gameObject);
    }

    private void StoryMode()
    {
        GameController.Instance().archiveCtr.levelData.gameType = GameType.Story;
        GameController.Instance().mapCtr.CreateChildMaps();
        // GameController.Instance().mapCtr.CreateStoryMap();
        GameController.Instance().playerCtr.CreatePlayer();
        GameController.Instance().weaponCtr.CreateWeapon();

        GameController.Instance().archiveCtr.CreateStoryArchive();
        Destroy(gameObject);
    }

    private void Setting()
    {
        GameController.Instance().panelCtr.LoadSettingPanel();
    }

    private void OpenCharacter()
    {
        GameController.Instance().panelCtr.LoadCharacterPanel();
    }

    private void ExitGame()
    {
        Application.Quit();
    }
}